package com.tanhua.dubbo.server.api;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.tanhua.dubbo.server.pojo.Users;
import com.tanhua.dubbo.server.vo.PageInfo;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

/**
 * @author 陈功炳
 * @since 2021/12/3 20:00
 */
@Service(version = "1.0.0")
public class UsersApiImpl implements UsersApi{

    @Autowired
    private MongoTemplate mongoTemplate;


    /**
     * 保存好友关系
     *
     * @param userId   用户id
     * @param friendId 好友id
     * @return
     */
    @Override
    public String saveUsers(Long userId, Long friendId) {
        if (!ObjectUtil.isAllNotEmpty(userId,friendId)){
            return null;
        }

        //检测是否该好友关系是否存在
        Query query = Query.query(Criteria.where("userId").is(userId)
        .and("friendId").is(friendId));
        long count = mongoTemplate.count(query, Users.class);
        if (count>0){
            return null;
        }

        Users users = new Users();
        users.setId(ObjectId.get());
        users.setUserId(userId);
        users.setFriendId(friendId);
        users.setDate(System.currentTimeMillis());

        //注册我与好友的关系
        mongoTemplate.save(users);

        //注册好友与我的关系
        users.setId(ObjectId.get());
        users.setUserId(friendId);
        users.setFriendId(userId);
        mongoTemplate.save(users);

        return users.getId().toHexString();
    }

    /**
     * 删除好友数据
     *
     * @param userId   用户id
     * @param friendId 好友id
     * @return
     */
    @Override
    public Boolean removeUsers(Long userId, Long friendId) {
        Query query1 = Query.query(Criteria.where("userId").is(userId)
        .and("friendId").is(friendId));

        //删除我与好友的关系数据
        long count1 = mongoTemplate.remove(query1, Users.class).getDeletedCount();

        Query query2 =Query.query(Criteria.where("userId").is(friendId)
        .and("friendId").is(userId));

        //删除好友与我的关系数据
        long count2 = mongoTemplate.remove(query2, Users.class).getDeletedCount();

        return count1 > 0 && count2 > 0;
    }

    /**
     * 根据用户id查询全部Users列表
     * @param userId
     * @return
     */
    @Override
    public List<Users> queryAllUsersList(Long userId) {
        Query query = Query.query(Criteria.where("userId").is(userId));
        return mongoTemplate.find(query,Users.class);
    }

    /**
     * 根据用户id查询Users列表（分页查询）
     * @param userId
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<Users> queryUsersList(Long userId, Integer page, Integer pageSize) {
        PageRequest pageRequest = PageRequest.of(page - 1,pageSize, Sort.by(Sort.Order.desc("created")));

        Query query = Query.query(Criteria.where("userId").is(userId)).with(pageRequest);

        List<Users> usersList = mongoTemplate.find(query,Users.class);

        PageInfo<Users> pageInfo = new PageInfo<>();
        pageInfo.setPageNum(page);
        pageInfo.setPageSize(pageSize);
        pageInfo.setRecords(usersList);

        return pageInfo;
    }
}
